﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.Threading;
using Microsoft.Win32;

namespace WallpaperControl
{
    static class Program
    {
        /// <summary>
        /// Responsible for reading and writing of the configuration files.
        /// </summary>
        internal static CfgFileManager CfgFileManager = new CfgFileManager();

        /// <summary>
        /// Holds the application configuration.
        /// </summary>
        internal static Configuration Configuration = new Configuration();

        /// <summary>
        /// The current session.
        /// </summary>
        internal static Session CurrentSession = new Session();

        /// <summary>
        /// Manages the applications start mode.
        /// </summary>
        internal static StartManager StartManager = new StartManager();

        /// <summary>
        /// Holds resources for internationalized strings.
        /// </summary>
        internal static ResourceManager Lang = new ResourceManager("WallpaperControl.Language", Assembly.GetExecutingAssembly());

        /// <summary>
        /// The TrayApp
        /// </summary>
        internal static TrayApp TrayApp = new TrayApp();

        /// <summary>
        /// The ControlPanel
        /// </summary>
        internal static ControlPanel ControlPanel;

        /// <summary>
        /// Holds the version of the application as stored in the registry. Default is "unknown".
        /// </summary>
        internal static string Version
        {
            get
            {
                return (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\WallpaperControl", "Version", Program.Lang.GetString("Version_Unknown"));
            }
        }

        /// <summary>
        /// Main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
            // Load settings from file
            Program.CfgFileManager.LoadSettingsFile();
            // Open ControlPanel if requested
            Program.ControlPanel = new ControlPanel();
            if (Program.Configuration.StartWithControlPanel)
            {
                Program.EnterControlPanel();
            }
            // Load saved images from file
            Program.CfgFileManager.LoadImageListFile();
            // Load saved condition from file
            Program.CfgFileManager.LoadConditionFile();
            // Enter the TrayApp
            Program.TrayApp.Enter();
            // Restore sesson
            Program.CfgFileManager.LoadSessionFile();
            // Update ControlPanel to loaded configuration
            if (Program.Configuration.StartWithControlPanel)
            {
                Program.ControlPanel.RestoreValues();
            }
            // Start message loop (no form - ControlPanel is optional)
            Application.Run();
        }

        /// <summary>
        /// Checks if the OS supports other images than bitmaps as wallpaper.
        /// </summary>
        /// <returns>True if more image formats are supported, false otherwise.</returns>
        internal static bool HasAdvancedImgSupport()
        {
            bool advancedImgSupport;
            if (Environment.OSVersion.Version >= new Version(6, 0))
            {
                advancedImgSupport = true;
            }
            else
            {
                advancedImgSupport = false;
            }
            return advancedImgSupport;
        }
        
        /// <summary>
        /// Enters the ControlPanel. This mode allows users to configure WallpaperControl.
        /// </summary>
        internal static void EnterControlPanel()
        {
            if (ControlPanel.IsOpen && ControlPanel.Visible)
            {
                Program.ControlPanel.Select();
                Program.ControlPanel.WindowState = FormWindowState.Normal;
                Program.ControlPanel.BringToFront();

            }
            else
            {
                if (Program.ControlPanel.IsDisposed)
                {
                    Program.ControlPanel = new ControlPanel();
                }
                Program.ControlPanel.Show();
            }
        }

        /// <summary>
        /// Holds the extensions of all supported image formats.
        /// </summary>
        internal static string[] SupportedImages;
    }
}
